/**
 * 主页面
 */

var ssoUrl = "${sso.url}";
var webUrl = "${web.url}";

//文件上传服务器地址
var fileServer = "${fileupload.url}";

//上传文件地址
var uploadUrl = fileServer + "upload";
//获取上传进度地址
var uploadProgress = fileServer+"progress";
//下载文件地址
var downloadUrl = fileServer + "download";
//浏览文件地址
var viewUrl = fileServer + "view";
//用于切图地址
var viewImgUrl = fileServer + "imageView";

$(function(){
	modulefunc();
	//getUser();
	navTabAnimations();
	loadNav();
	
	$(".dropdown").on("mouseover",function(){$(this).find(".dropdown-menu").show();});
	$(".dropdown").on("mouseout",function(){$(this).find(".dropdown-menu").hide(); });
	
	//全局ajax处理
	$.ajaxSetup({
		//发送请求前触发
		beforeSend: function (xhr) {
			//可以设置自定义标头
			xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
		},
		error:function(xhr,status,error){
			if(xhr.status==403)
			{
				alert('无权限访问');
			}
			if(xhr.status==401)
			{
				location.href = webUrl;
			}
		}
	});
});

//加载导航
function loadNav(){
	var params = {};
	params.level = 1;
	$.ajax({
		url: webUrl+'sysmenu/findSysMenu.notPermSc',
		data:params
	}).done(function(data){
		//alert(JSON.stringify(data));
		var result = data.bo;
		$('#nav_list').empty();
		for(var i=0;i<result.length;i++)
		{
			var nav = '<div class="manage_list"><div class="manage_item"><i class="'+result[i].icon+'"></i><span class="manage_type">'+result[i].name+'</span></div>'
			+'<ul class="sub_manage_list" id='+result[i].id+'></ul>'
			+'</div>';
			getSubNav(result[i].id);
			$('#nav_list').append(nav);
		}
	});
}

//获得子菜单
function getSubNav(parentId){
	var params = {};
	params.level = 2;
	params.parentId = parentId;
	$.ajax({
		url: webUrl+'sysmenu/findSysMenu.notPermSc',
		data:params
	}).done(function(data){
		var result = data.bo;
		for(var i=0;i<result.length;i++){
			var subNav = '<li class="sub_manage_item" go-page="'+result[i].url+'">'
				/*+'<b class="'+result[i].icon+'" data-tipso="'+result[i].name+'"></b>'*/
				+'<i class="'+result[i].icon+'"></i>'
				+'<span class="manage_link" >'+result[i].name
				+'</span></li>';
			$('#'+parentId).append(subNav);
		}
	});
}

//点击左侧导航动画效果
function navTabAnimations(){
	//左侧导航展开
	$('#nav_list').on('click','.manage_item',toggleFold);

	function toggleFold(){
		if(!$(this).hasClass("curr")){
			$(this).addClass("curr").
					next(".sub_manage_list").slideDown();	
					
			$(this).parents(".manage_list").siblings().
					find(".manage_item").removeClass("curr");	
					
			$(this).parents(".manage_list").siblings().
					find(".sub_manage_list").slideUp();	
		}else{
			$(this).removeClass("curr").
			next(".sub_manage_list").slideUp();	
		};		
	};

	//收起左侧
	$("#fold_sider").on("click",function(){
		if(!$(".sider_panel").hasClass("fold")){
			$(this).addClass("reverse");
			$(".sider_panel").addClass("fold");
			$(".main_panel").css({"margin-left":"45px"});
		}else{
			$(this).removeClass("reverse");
			$(".sider_panel").removeClass("fold");
			$(".main_panel").css({"margin-left":"220px"});	
		}
		
		$("[id*='Grid']").datagrid('resize',{width:'auto'});
		
	});
	
	//导航绑定点击事件
	$('#nav_list').on('click','.sub_manage_item', function(){
		$(".sub_manage_item").removeClass('active');
		$(this).addClass('active');
		loadMainPage($(this).attr('go-page'));
	});
	
	//左侧绑定鼠标提示事件
	//$('.main_panel').on('mouseover mouseout','.tips',function(){$('.tips').tipso({useTitle: false})});
}

//load主页面
function loadMainPage(url){
	if(url!='')
	{
		$('.main_panel').load(
			url
		);
	}
}

//过滤权限
var modulefunc = function(){
//	$.ajax({
//		url: webUrl+'sysPermission/findModulePermission.notPermSc'
//	}).done(function(data){
//		$('head').append(data);
//	});
}

//获取用户信息
/*function getUser(){
	$.ajax({
		url: webUrl+'sysUser/getCurrentUser.notPermSc',
	}).done(function(data){
		var result = data.bo;
		$('#main-user').text(result.username);
		//退出
		var logout = ssoUrl + 'logout?service='+webUrl;
		$('#signout a').attr('href',logout);
	});
}*/

